<?php

class TODAYDataParser extends DOMDataParser {

    public function parseData($data)
    {
        $data = '<!DOCTYPE html>' . $data;
        $htmlId = $this->getOption('htmlId');
        $city = $this->getOption('detail_city');
        $arrCity = explode(",", $city);
        $dom = new DOMDocument();
        /* there might be errors, who knows what we're getting */
        if (!@$dom->loadHTML(mb_convert_encoding($data, 'HTML-ENTITIES', $this->encoding))) {
            $dom = false;
        } else {
            $dom = $this->rewriteAbsoluteUrls($dom);
        }

        $contents = array();
        if ($dom) {
            if ($weather = $dom->getElementById($htmlId)) {
                $elements = $weather->getElementsByTagName('tr');
                $element1 = $elements->item(0)->getElementsByTagName('th');//0 1 2 3 4 5 6 7 8 9
                foreach ($elements as $i => $arr) {
                    if (strpos($dom->saveXML($arr), $arrCity[1])) {
                        $element2 = $elements->item($i)->getElementsByTagName('td');//0 1 2 3 4 5 6 7 8
                        foreach ($element2 as $i => $element) {
                            $content[1] = $dom->saveXML($element1->item($i+1));
                            $content[0] = $dom->saveXML($element);
                            if ($i == 0) {
                                $img = $element->getElementsByTagName('img');
                                $img1 = $img->item(0);
                                $content[2] = $img1->getAttribute("title");
                            }
                            $contents[] = $content;
                        }
                    }
                }
            }
        }
        return $contents;
    }
}
